package ua.kharkov.khpi.tarasenko.bach.dao.core;

import java.sql.Connection;

import org.apache.log4j.Logger;

import ua.kharkov.khpi.tarasenko.bach.core.EShopException;
import ua.kharkov.khpi.tarasenko.bach.core.db.EShopDataStoreException;
import ua.kharkov.khpi.tarasenko.bach.dao.core.category.CategoryDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.item.ItemDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.order.OrderItemDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.order.OrderUserDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.user.UserDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.usersession.UserSessionDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.mysql.MysqlDAOFactory;

/**
 * Provides ability to create DAO factories for specified DAO types.
 * 
 * @author oleksandr
 * 
 */
public abstract class DAOFactory {
	private static final Logger log = Logger.getLogger(DAOFactory.class);

	public static enum DAO_TYPE {
		MYSQL, DERBY;
	}

	/**
	 * Creates an object of class that implements UserDAO of specified data
	 * source type.
	 * 
	 * @param conn
	 *            - connection to the database.
	 * @return - instance of class that implements UserDAO.
	 * @throws EShopDataStoreException
	 *             if connection is null.
	 */
	public abstract UserDAO getUserDAO(Connection conn)
			throws EShopDataStoreException;

	/**
	 * Creates an object of class that implements ItemDAO of specified data
	 * source type.
	 * 
	 * @param conn
	 *            - connection to the database.
	 * @return - instance of class that implements ItemDAO.
	 * @throws EShopDataStoreException
	 *             if connection is null.
	 */
	public abstract ItemDAO getItemDAO(Connection conn)
			throws EShopDataStoreException;

	/**
	 * Creates an object of class that implements CategoryDAO of specified data
	 * source type.
	 * 
	 * @param conn
	 *            - connection to the database.
	 * @return - instance of class that implements CategoryDAO.
	 * @throws EShopDataStoreException
	 *             if connection is null.
	 */
	public abstract CategoryDAO getCategoryDAO(Connection conn)
			throws EShopDataStoreException;

	/**
	 * 
	 * @param conn
	 *            - connection to the database.
	 * @return - instance of class that implements OrderUserDAO.
	 * @throws EShopDataStoreException
	 *             if connection is null.
	 */
	public abstract OrderUserDAO getOrderUserDAO(Connection conn)
			throws EShopDataStoreException;

	/**
	 * 
	 * @param conn
	 *            - connection to the database.
	 * @return - instance of class that implements UserSessionDAO.
	 * @throws EShopDataStoreException
	 *             if connection is null.
	 */
	public abstract UserSessionDAO getUserSessionDAO(Connection conn)
			throws EShopDataStoreException;

	/**
	 * 
	 * @param conn
	 *            - connection to the database.
	 * @return - instance of class that implements OrderItemDAO.
	 * @throws EShopDataStoreException
	 *             if connection is null.
	 */
	public abstract OrderItemDAO getOrderItemDAO(Connection conn)
			throws EShopDataStoreException;

	/**
	 * Creates DAOFactory for specified DAO type.
	 * 
	 * @param type
	 *            - DAO type to create DAO factory.
	 * @return DAO factory for DAO type.
	 * @throws EShopException
	 *             if DAO tytypepe is not specified.
	 */
	public static DAOFactory getDAOFactory(DAO_TYPE type) throws EShopException {
		if (log.isTraceEnabled()) {
			log.trace("DAO type to create is " + type);
		}
		switch (type) {
		case MYSQL:
			if (log.isDebugEnabled()) {
				log.debug("created new MysqlDAOFactory");
			}

			return new MysqlDAOFactory();

		default:
			throw new EShopException("DAO type " + type + " is not specified!");
		}
	}
}
